/**
 * Copyright  (c) 2009-2010 Wilmar IT Service,  Inc.   
 * All  rights  reserved. 
 */
package com.wilmar.process.dao.procedure.impl;

import java.lang.reflect.InvocationTargetException;
import java.sql.Array;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import oracle.jdbc.OracleTypes;

import com.wilmar.process.dao.procedure.api.ITaskInstanceProcedureProxy;
import com.wilmar.process.entity.query.ProcessTakenTaskQueryObject;
import com.wilmar.process.entity.vo.TaskInstanceVO;
import com.wilmar.process.enumerate.EnumDatabaseDataType;

/**
 * @author MaKuBEX Email:makubexnet@gmail.com
 * @version 1.0.0 Apr 21, 2010
 * @since 1.0.0
 * 
 */
public class TaskInstanceProcedureProxyImpl extends
		BaseProcedureProxyImpl<TaskInstanceVO> implements
		ITaskInstanceProcedureProxy {

	/**
	 * @see com.wilmar.process.dao.procedure.api.ITaskInstanceProcedureProxy#getTakenTaskListByQueryObject(com.wilmar.process.entity.query.ProcessTakenTaskQueryObject)
	 */
	public List<TaskInstanceVO> getTakenTaskListByQueryObject(
			ProcessTakenTaskQueryObject queryObject) {
		Connection con = null;
		try {
			con = super.getConnectioin();
			ProcessTakenTaskQueryObject qobj = new ProcessTakenTaskQueryObject();
			qobj.setProcessDefinitionId("123");
			qobj.setTaskName("hahaha");
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("processDefinitionId", EnumDatabaseDataType.ARRAY_VARCHAR
					.getType());
			map.put("taskName", EnumDatabaseDataType.ARRAY_VARCHAR.getType());
			Map<String, Object> pms = qobj.format(map, con);
			String proc = "{call TESTPROC(?,?,?)}";
			CallableStatement st = con.prepareCall(proc);
			st.setArray(1, (Array) pms.get("processDefinitionId"));
			st.setArray(2, (Array) pms.get("taskName"));
			st.registerOutParameter(3, OracleTypes.CURSOR);
			st.execute();
			ResultSet rs = (ResultSet) st.getObject(3);
			while (rs.next()) {
				System.out.println(rs.getString(1));
			}
			con.commit();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} finally {
			try {
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		return null;
	}

}
